package csu.webcourse.mypetstore.persistence.impl;

import csu.webcourse.mypetstore.domain.*;
import csu.webcourse.mypetstore.persistence.CartDao;
import csu.webcourse.mypetstore.persistence.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class CartDaoImpl implements CartDao {

    private static final String GET_CART =
            "SELECT ITEMID AS itemId FROM CART WHERE USERID = ?";

    private static final String INSERT_CART_LIST = "INSERT INTO CART" +
            " (ITEMID,USERID)" +
            " VALUES" +
            " (?, ?)";

    private static final String REMOVE_CART_LIST ="DELETE FROM CART WHERE USERID = ?";

    private static final String REMOVE_CART_LIST_ASYNC ="DELETE FROM CART WHERE USERID = ? AND ITEMID = ?";
    @Override
    public ArrayList<String> getCart(String userId) {
        ArrayList<String>itemIds=new ArrayList<String>();
        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(GET_CART);
            preparedStatement.setString(1, userId);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                String itemId=resultSet.getString("itemId");
                itemIds.add(itemId);
            }
            DBUtil.closeResultSet(resultSet);
            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return itemIds;
    }

    @Override
    public void insertCartList(String itemId, String userId) {
        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(INSERT_CART_LIST);
            preparedStatement.setString(1,itemId);
            preparedStatement.setString(2, userId);
            preparedStatement.executeUpdate();
            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void removeCartList(String userId) {
        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(REMOVE_CART_LIST);
            preparedStatement.setString(1, userId);
            preparedStatement.executeUpdate();
            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void removeCartListAsync(String userId, String itemId) {
        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(REMOVE_CART_LIST_ASYNC);
            preparedStatement.setString(1, userId);
            preparedStatement.setString(2,itemId);
            preparedStatement.executeUpdate();
            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}
